#include <stdio.h>
void swap(int* a, int* b)
{
	int tmp = *a;
	*a = *b;
	*b = tmp;



}
void SelectSort(int* a, int n)
{
	int begin = 0;
	int end = n - 1;
	while (begin < end)
	{
		int maxi = begin;
		int mini = begin;
		for (int i = begin + 1; i <=end; i++)
		{
			if (a[i] > a[maxi])

			{
				maxi = i;
			}
			if (a[i] < a[mini])
			{
				mini = i;
			}
         }
		swap(&a[begin], &a[mini]);
		if (maxi == begin)
		{
			maxi = mini;
		}
		swap(&a[end], &a[maxi]);
		begin++;
		end--;












	}
	









}
void AdjustDwon(int* a, int n, int root)
{
	int child = root * 2 + 1;
	while (child < n)
	{
		if (child+1<n &&a[child] < a[child + 1])
		{
			child = child + 1;
		}



		if (a[child] > a[root])
		{
			swap(&a[child], &a[root]);
			root = child;
			child = root * 2 + 1;



		}
		else
		{
			break;
		}








	}



}
void HeapSort(int* a, int n)
{


	for (int i = (n - 1 - 1) / 2; i >= 0; i--)
	{
		AdjustDwon(a, n, i);
	}
	int end = n - 1;
	while (end > 0)
	{
		swap(&a[0], &a[end]);
		AdjustDwon(a, end, 0);
		end--;



	}





}
void BubbleSort(int* a, int n)
{
	for (int i = 0; i < n-1; i++)
	{
		for (int j = 0; j < n - i-1; j++)
		{
			if (a[j + 1] < a[j])
			{
				swap(&a[j], &a[j + 1]);
			}


        }







	}





}
void InsertSort(int* a, int n)
{
	
	
	for (int i = 0; i < n-1; i++)
	{
		int end = i;
		int tmp = a[end + 1];

		while (end >=0)
		{
			if (tmp < a[end])
			{
				a[end + 1] = a[end];
				end--;

			}
			else
			{
				break;
			}





		}
		a[end + 1] = tmp;







	}












}
void ShellSort(int* a, int n)
{


	int gap = 3;

	while (gap >= 1)
	{
		gap /= 2;
	
			for (int i = 0; i < n - gap; i++)
			{
				int end = i;
				int tmp = a[end + gap];

				while (end >= 0)
				{

					if (tmp < a[end])
					{
						a[end + gap] = a[end];
						end -= gap;
					}
					else
					{
						break;
					}







				}
				a[end + gap] = tmp;










			}
		}
	}
		
void TestOP()
{
	srand(time(0));
	const int N = 100000;
	int* a1 = (int*)malloc(sizeof(int) * N);
	int* a2 = (int*)malloc(sizeof(int) * N);
	int* a3 = (int*)malloc(sizeof(int) * N);
	int* a4 = (int*)malloc(sizeof(int) * N);
	int* a5 = (int*)malloc(sizeof(int) * N);
	int* a6 = (int*)malloc(sizeof(int) * N);
	for (int i = 0; i < N; ++i)
	{
		a1[i] = rand();
		a2[i] = a1[i];
		a3[i] = a1[i];
		a4[i] = a1[i];
		a5[i] = a1[i];
		a6[i] = a1[i];

		
		
	}
	int begin1 = clock();
	InsertSort(a1, N);
	int end1 = clock();
	int begin2 = clock();
	ShellSort(a2, N);
	int end2 = clock();
	int begin3 = clock();
	SelectSort(a3, N);
	int end3 = clock();
	int begin4 = clock();
	HeapSort(a4, N);
	int end4 = clock();
	int begin5 = clock();
	QuickSort(a5, 0, N - 1);
	int end5 = clock();
	int begin6 = clock();
	MergeSort(a6, N);
	int end6 = clock();
	printf("InsertSort:%d\n", end1 - begin1);
	printf("ShellSort:%d\n", end2 - begin2);
	printf("SelectSort:%d\n", end3 - begin3);
	printf("HeapSort:%d\n", end4 - begin4);
	printf("QuickSort:%d\n", end5 - begin5);
	printf("MergeSort:%d\n", end6 - begin6);
	free(a1);
	free(a2);
	free(a3);
	free(a4);
	free(a5);
	free(a6);
}
















int main()
{
	//int arr[] = { 10,8,5,2,7,4,3,6,9,1 };
	//ShellSort(arr, 10);
	//InsertSort(arr, 10);
	//SelectSort(arr, 10);
	TestOP();
	//HeapSort(arr, 10);
	/*for (int i = 0; i < 10; i++)
	{
		printf("%d ", arr[i]);


	}*/



}



